'use strict';

const Service = require('egg').Service;
//
const top = '顶级菜单'
class MenuService extends Service {
  //查询菜单管理级权限
  async getmenutotal() {
    try {
      const { ctx, app } = this;
      let data = await app.model.Menufather.findAll({
        order: [['sorting', 'DESC']],
        include: [{
          model: app.model.Menuson,
          separate: true,
          order: [
            ['sorting', 'DESC']
          ],
          attributes: {
            exclude: ['menufatherName'] //排除此字段返回其余字段
          },
        }]
      })
      return {
        code: 20000,
        data
      };
    } catch (error) {
      return await this.app.Lostconnection(error.message);
    }
  }
  //删除父级菜单----------------------------------------------------------------------------------------------------------------
  async delfather(name) {
    try {
      const son = await this.app.model.Menuson.findAll({ where: { menufather_name: name.name } });
      console.log(son.length);
      if (son.length > 0) {
        return {
          code: 20002,
          msg: '此菜单下有子菜单存在，请删除子菜单'
        };
      }
      const data = await this.app.model.Menufather.destroy({ where: name });
      return {
        code: 20000,
        msg: '删除成功',
        data
      };
    } catch (error) {
      return await this.app.Lostconnection(error.message);
    }
  }
  //删除子级菜单
  async delson(name) {
    try {
      const data = await this.app.model.Menuson.destroy({ where: name });
      return data;
    } catch (error) {
      return await this.app.Lostconnection(error.message);
    }
  }
  //添加菜单的父级数据----------------------------------------------------------------------------------------------------------------
  async getmenuname() {
    try {
      let data = await this.app.model.Menufather.findAll({
        attributes: ['name'],
        order: [['sorting', 'DESC']]
      });
      data.unshift({ name: top });
      return {
        code: 20000,
        data
      };
    } catch (error) {
      return await this.app.Lostconnection(error.message);
    }
  }
  //添加菜单----------------------------------------------------------------------------------------------------------------
  async cremenu(cre) {
    try {
      if (cre.menufather_name == top) {
        await this.app.model.Menufather.create(cre);
      } else {
        await this.app.model.Menuson.create(cre);
      }
      return {
        code: 20000,
        msg: '成功'
      }
    } catch (error) {
      return await this.app.Lostconnection(error.message);
    }
  }
  //修改子级菜单----------------------------------------------------------------------------------------------------------------
  async sonupdate(body) {
    try {
      if (body.menufather_name == top) {
        const data = await this.ctx.service.menu.cremenu(body);
        if (data.code == 20000) {
          const msg = await this.ctx.service.menu.delson({ name: body.jname });
          return {
            code: 20000,
            msg,
          };
        } else {
          return {
            code: 20002,
            data
          };
        }
      } else {
        const data = await this.app.model.Menuson.update(body, { where: { name: body.jname } });
        return {
          code: 20000,
          data
        };
      }
    } catch (error) {
      return await this.app.Lostconnection(error.message);
    }
  }
  //修改父级菜单
  async fatherupdate(body) {
    try {
      if (body.menufather_name == top) {
        const data = await this.app.model.Menufather.update(body, { where: { name: body.jname } });
        return {
          code: 20000,
          data
        };
      } else {
        const son = await this.app.model.Menuson.findAll({ where: { menufather_name: body.jname } });
        if (son.length > 0) {
          return {
            code: 20002,
            msg: '此菜单下有子菜单存在，请删除子菜单'
          };
        } else {
          const data = await this.ctx.service.menu.cremenu(body);
          if (data.code == 20000) {
            const msg = await this.ctx.service.menu.delfather({ name: body.jname });
            return {
              code: 20000,
              msg,
            };
          } else {
            return {
              code: 20002,
              data
            };
          }
        }
      }
    } catch (error) {
      return await this.app.Lostconnection(error.message);
    }
  }
}

module.exports = MenuService;